﻿using DotSpatial.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Demo
{
    public partial class AttributeForm : Form
    {
        public AttributeForm(IFeature feature)
        {
            InitializeComponent();
            this.feature = feature;
        }
        private IFeature feature;

        private void bindFeatureFields(IFeature f)
        {
            if (f == null)
            {
                this.dgvAttributes.DataSource = null;
                return;
            }
            var dt = new DataTable();
            DataColumn column = dt.Columns.Add("Field Name");
            column.Caption = "属性名称";
            column = dt.Columns.Add("Value");
            column.Caption = "属性值";

            column = dt.Columns.Add("FieldType");
            column.Caption = "字段类型";
            if (f.DataRow == null)
            {
                f.ParentFeatureSet.FillAttributes();
            }
            var columns = f.ParentFeatureSet.GetColumns();
            foreach (var fld in columns)
            {
                var dr = dt.NewRow();
                dr["Field Name"] = fld.ColumnName;

                if (f.DataRow != null)
                {
                    dr["Value"] = f.DataRow[fld.ColumnName];
                    dr["FieldType"] = f.DataRow.Table.Columns[fld.ColumnName].DataType.ToString();
                }
                dt.Rows.Add(dr);
            }
            dgvAttributes.DataSource = dt;
            for (int i = 0; i < dgvAttributes.Columns.Count; i++)
            {
                dgvAttributes.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                dgvAttributes.Columns[i].HeaderText = dt.Columns[i].Caption;
                dgvAttributes.Columns[i].ValueType = dt.Columns[i].DataType;
                if (i == 1)
                {
                    dgvAttributes.Columns[i].ReadOnly = false;
                }
                else
                {
                    dgvAttributes.Columns[i].ReadOnly = true;
                }
            }

            //  dgvAttributes.Parent.Width = dgvAttributes.Width;
        }

        private void BtnReset_Click(object sender, EventArgs e)
        {
            this.bindFeatureFields(this.feature);
            this.btnReset.Enabled = false;
            this.btnOK.Enabled = false;
        }

        private void BtnOK_Click(object sender, EventArgs e)
        {
            if (this.feature == null)
            {
                return;
            }
            for (int i = 0; i < dgvAttributes.Rows.Count; i++)
            {
                string fieldName = dgvAttributes.Rows[i].Cells["Field Name"].Value.ToString();
                Type dataType = Type.GetType(dgvAttributes.Rows[i].Cells["FieldType"].Value.ToString());
                object cellValue = dgvAttributes.Rows[i].Cells["Value"].Value;
                if (cellValue != DBNull.Value)
                {
                    if (dataType != typeof(DBNull))
                    {
                        cellValue = Convert.ChangeType(cellValue, dataType);
                    }
                }
                if (this.feature.DataRow[fieldName] != cellValue)
                {
                    this.feature.DataRow[fieldName] = cellValue;
                }
            }
            this.feature.SaveToModel();
            this.btnOK.Enabled = false;
            this.btnReset.Enabled = false;
        }


        private void DgvAttributes_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0)
            {
                return;
            }
            if (this.feature == null)
            {
                return;
            }
            string fieldName = this.dgvAttributes.Rows[e.RowIndex].Cells["Field Name"].Value.ToString();
            object rtValue = this.feature.DataRow[fieldName];
            object gridValue = this.dgvAttributes.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
            if (rtValue == null && gridValue != null)
            {
                this.btnOK.Enabled = true;
                this.btnReset.Enabled = true;
                return;
            }

            string strRt = rtValue.ToString();
            string strGrid = gridValue == null ? "" : gridValue.ToString();

            if (!strRt.Equals(strGrid))
            {
                this.btnOK.Enabled = true;
                this.btnReset.Enabled = true;
                return;
            }
            this.btnOK.Enabled = false;
            this.btnReset.Enabled = false;
        }

        private void AttributeForm_Load(object sender, EventArgs e)
        {
            this.bindFeatureFields(this.feature);
        }
    }
}
